<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
	protected $page_data;
	
	public function __construct() {
		parent::__construct();
		$this->page_data = array();
		//$this->_auto_login();
		$this->_init_login_data();
		if (!$this->input->is_ajax_request()) {
			$this->_build_common_page_part();
		}
	}
	
	private function _init_login_data() {
		$this->page_data['login_user_id'] = (int) $this->session->userdata(KEY_USER_ID);
		$this->page_data['login_user_email'] = (int) $this->session->userdata(KEY_USER_EMAIL);
		$this->page_data['login_user_name'] = (string) $this->session->userdata(KEY_USER_NAME, TRUE);
		$this->page_data['login_user_level'] = (int) $this->session->userdata(KEY_USER_LEVEL);
	}
	
	private function _build_common_page_part() {
		$this->load->model('category_model');
		$category_list = $this->category_model->get_list();
		$this->page_data['category_list'] = $category_list;
	}
	
	protected function _check_ajax_login() {
		if ($this->page_data['login_user_id'] <= 0) {
			$ret = array('status' => STATUS_ERROR, 'msg' => '请先登录！');
			echo json_encode($ret); exit;
		}
	}
	
	protected function _check_page_login() {
		if ($this->page_data['login_user_id'] <= 0) {
			redirect(site_url());
		}
	}
}